package main

import (
	"fmt"
	"time"
)
/*chan<-  表示外面channel只能发数据，不能收
	<-chan 表示外面的channel只能收数据，不能发
*/
func work(i int,c chan int){
	for  n := range c {
		fmt.Printf("%d,%c\n",i,n)
	}
}
func createWorker(i int ) chan<- int{
	c := make(chan int)
	go work(i,c)
	return c
}
func chanDemo(){
	var cs  [10]chan<- int
	for i :=0; i<10; i++{
		cs[i] = createWorker(i)
	}
	/*往channel中发数据*/
	for i := 0; i<10 ;i++  {
		cs[i] <- 'a'+i
	}
	for i := 0; i<10 ;i++  {
		cs[i] <- 'A'+i
	}

	time.Sleep(time.Minute)
}

/*------------------------------------------------------------------*/
// 关闭channel 一定是发送方close
func channelClose(){
	var c = make(chan int,3)
	go work(0,c)
	c <- 'a'
	c <- 'b'
	c <- 'd'
	c <- 'e'
	// 关闭channel
	close(c)
	time.Sleep(time.Minute)
}

func bufferedChannel(){
	var c = make(chan int,3)
	go work(0,c)
	c <- 'a'
	c <- 'b'
	c <- 'd'
	c <- 'e'
}

func main() {
	channelClose()
}
